package com.rmondjone.androidcamerademo

import android.util.Log
import android.widget.Toast
import com.ant.http.Bean.HttpResult
import com.ant.http.HttpManager
import com.rmondjone.androidcamerademo.bean.FaceBean
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.File

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:
 */
object MainViewModel {


    fun upFile(file: File, typeId: Int, callback: OnCallListener) {

        val filBody = file.asRequestBody("file/*".toMediaType())
        val part = MultipartBody.Part.createFormData("file", file.name, filBody)

        HttpManager.getService(CommonService::class.java).upFile(part).enqueue(object : retrofit2.Callback<HttpResult<FileBean>> {
            override fun onFailure(call: Call<HttpResult<FileBean>>, t: Throwable) {
//                Log.e("msg", "上传----------- 失败")
                t.printStackTrace()
//                Toast.makeText(MyApplication.application, "识别失败", Toast.LENGTH_SHORT).show()
                callback.onFail("网络不稳当")

            }

            override fun onResponse(call: Call<HttpResult<FileBean>>, response: Response<HttpResult<FileBean>>) {
                Log.e("msg", "上传----------- 上传成功- " + response.body()?.data)


                response.body()?.let {
                    if (it.code == 1 && it.data != null) {
                        seeinfo(it.data?.full_url ?: "", typeId, callback)
                    } else {

//                        Toast.makeText(MyApplication.application, it.message
//                                ?: "识别失败", Toast.LENGTH_SHORT).show()

                        callback.onFail(it.message
                                ?: "网络不稳当")

                    }

                    return

                }
//                Toast.makeText(MyApplication.application, "识别失败", Toast.LENGTH_SHORT).show()

                callback.onFail("网络不稳当")


//                Toast.makeText(MyApplication.application, response.body()?.message
//                        ?: "", Toast.LENGTH_SHORT).show()
            }

        })

    }


    fun seeinfo(url: String, typeId: Int, callback: OnCallListener) {
        HttpManager.getService(CommonService::class.java).seeinfo(url, typeId).enqueue(object : Callback<HttpResult<FaceBean>> {
            override fun onFailure(call: Call<HttpResult<FaceBean>>, t: Throwable) {
//                Log.e("msg", "------------- 错误   " + t.message)
//                Toast.makeText(MyApplication.application, "识别失败", Toast.LENGTH_SHORT).show()

                t.printStackTrace()
                callback.onFail("网络不稳当")


            }

            override fun onResponse(call: Call<HttpResult<FaceBean>>, response: Response<HttpResult<FaceBean>>) {

                response.body()?.let {
                    Log.e("msg", "------------- " + it.code + "  data-- " + it.data)

                    if (it.code == 1 && it.data != null) {
                        callback.onCallBack(it.data!!)
                    } else {
//                        Toast.makeText(MyApplication.application, it.message
//                                ?: "识别失败", Toast.LENGTH_SHORT).show()

                        callback.onFail(it.message ?: "网络不稳定")
                    }
                    return
                }
                callback.onFail("网络不稳定")

            }

        })
    }


    interface OnCallListener {

        fun onCallBack(data: FaceBean)
        fun onFail(message: String)


    }


}